package com.bonc.rms.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.bonc.odk.logmanager.LogManager;


public class PropertiesUtil {
	
	static Properties props = new Properties();
	
	static {
		InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties");
		try {
			props.load(in);
		} catch (IOException e) {
			LogManager.Exception("读取配置文件失败，请检查配置文件 config.properties 是否存在。", e);
			System.exit(1);
		} finally {
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
					LogManager.Exception("文件流关闭失败。", e);
					System.exit(1);
				}
			}
		}
	}
	
	public static String getString(String key, String defVal) {
		return props.getProperty(key) == null ? defVal : props.getProperty(key).trim();
	}
	
	public static String getString(String key) {
		return props.getProperty(key);
	}
	
	public static int getIntValue(String key, int defval) {
		int val = Integer.MIN_VALUE;
		String value = props.getProperty(key);
		if(value == null) {
			return defval;
		}
		try {
			val = Integer.parseInt(props.getProperty(key).trim());
		} catch (NumberFormatException e) {
			e.printStackTrace();
			LogManager.Exception("数字格式错误，请检查配置文件中的 " + key + " 值是否为数字", e);
			return defval;
		}
		return val;
	}
}
